package xyz.csmcool.imoss.controller;

import com.google.common.collect.Maps;
import io.minio.ListPartsResponse;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import xyz.csmcool.imoss.service.FileUploadService;
import xyz.csmcool.imutil.dto.FileUploadDto;
import xyz.csmcool.imutil.utils.ResultTool;
import xyz.csmcool.imutil.webVo.ResultVo;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.*;

/**
 * @author 什锦
 * @Package xyz.csmcool.imoss.controller
 * @since 2022/8/6 1:27
 */

@RestController
@RequestMapping("/upload")
@RequiredArgsConstructor
public class UploadFileController {

    final FileUploadService fileUploadService;

    private static ExecutorService threadPool = new ThreadPoolExecutor
            (1, 30, 60L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(30), new ThreadFactory() {
                @Override
                public Thread newThread(@NotNull Runnable r) {
                    return new Thread(r, "ThreadPool thread: ");
                }
            });

    @PostMapping("/update-avatar")
    @CrossOrigin
    public ResultVo<HashMap<String, String>> updateAvatar(@RequestParam("avatar") MultipartFile multipartFile) {
        String upload = fileUploadService.upload(multipartFile, "avatar/");
        HashMap<String, String> map = new HashMap<>(4);
        map.put("avatarUrl", upload);
        return ResultTool.success(map);
    }

    @PostMapping("/room-file")
    public ResultVo<HashMap<String, String>> uploadRoomFile(@RequestParam("file") MultipartFile multipartFile) {
        String upload = fileUploadService.upload(multipartFile, "chatRoom/");
        HashMap<String, String> map = new HashMap<>(4);
        map.put("url", upload);
        System.err.println("成功返回url" + upload);
        return ResultTool.success(map);
    }

    @PostMapping("/room-files")
    public ResultVo<HashMap<String, List<String>>> uploadRoomFiles(@RequestParam("file") MultipartFile[] multipartFile) {
        List<String> list = new LinkedList<>();
        // 利用线程池并发上传文件
        CompletableFuture.allOf(
                CompletableFuture.runAsync(()->{
                    if(multipartFile.length > 0) {
                        list.add(fileUploadService.upload(multipartFile[0], "chatRoom/image/"));
                    }
                },threadPool),
                CompletableFuture.runAsync(()->{
                    if(multipartFile.length > 1) {
                        list.add(fileUploadService.upload(multipartFile[1], "chatRoom/image/"));
                    }
                },threadPool),
                CompletableFuture.runAsync(()->{
                    if(multipartFile.length > 2) {
                        list.add(fileUploadService.upload(multipartFile[2], "chatRoom/image/"));
                    }
                },threadPool)
        ).join();
        HashMap<String, List<String>> map = Maps.newHashMapWithExpectedSize(1);
        map.put("url", list);
        System.err.println(list.size());
        return ResultTool.success(map);
    }

    @PostMapping("/createMultipartUpload")
    @CrossOrigin
    public ResultVo<Map<String, Object>> createMultipartUpload(@RequestBody FileUploadDto fileUploadDto) {
        Map<String, Object> multipartUpload = fileUploadService.
                createMultipartUpload(fileUploadDto.getFileName(), fileUploadDto.getChunkSize());
        return ResultTool.success(multipartUpload);
    }

    @GetMapping("/completeMultipartUpload")
    public ResultVo<HashMap<String, String>> completeMultipartUpload(@RequestParam("fileName") String fileName,
                                                                     @RequestParam("uploadId") String uploadId) {
        String url = fileUploadService.completeMultipartUpload(fileName, uploadId);
        HashMap<String, String> map = new HashMap<>(4);
        map.put("url", url);
        return ResultTool.success(map);
    }

}
